<?php
namespace app\spider\controller;
use app\spider\service\QueryFilmService;
use QL\QueryList;
use QL\Ext\CurlMulti;
use think\Db;

class Film
{
    public function fuckList()
    {
        $list = Db::table('db_film_site')->where('status', '=', 1)->select();
        $service = new QueryFilmService($list);
        $service->fuck();
    }


   public function buildIndex() {
        $es = \app\common\service\EsSearchService::getInstance();
        $offset = 0;
        $limit = 100;
        while (true) {
            $films = \app\common\model\FilmModel::where('id', '>=', 138940)->limit($offset, $limit)->select();
            if ($films->isEmpty()) {
                break;
            }
            $index = ['body' => []];
            foreach ($films as $key => $film) {
                $content = strip_tags($film['content']);
                $content = preg_replace("/\r|\n|\s+|\t/", ' ', $content);

//                $index['body'][] = [
//                    'index' => [
//                        '_index' => \app\common\service\EsSearchService::INDEX_NAME,
//                        '_type' => '_doc',
//                        '_id' => $film['id'],
//                    ],
//                ];
//                $index['body'][] = [
//                    'id'      => $film['id'],
//                    'title'   => $film['title'],
//                    'content' => $content,
//                    'status'  => $film['status'],
//                    'c_time'  => $film['c_time'],
//                    'site_id' => $film['site_id'],
//                ];
                $index['index'] = \app\common\service\EsSearchService::INDEX_NAME; //索引名称
                $index['id'] = $film['id'];
                $index['type'] = '_doc';
                $index['body'] = [
                    'id'      => $film['id'],
                    'title'   => $film['title'],
                    'content' => $content,
                    'status'  => $film['status'],
                    'c_time'  => $film['c_time'],
                    'site_id' => $film['site_id'],
                ];
                echo $key+1, "\n";
                $es->buildIndexOne($index);
            }
            // $es->bulkIndex($films);
            echo "\n======================\noffset:", $offset, "\n======================\n";
            $offset += $limit;
        }
    }


    public function fuckContent() {
        $json = '{"title":[".list_su ul li .liimg a.pic_link","title"],"url":[".list_su ul li .liimg a.pic_link","href"]}';

        $ql = QueryList::getInstance();
//        $data = $ql->get('http://www.btbtdy.me/btfl/dy1.html')->rules([
//            'title' => ['.list_su ul li .liimg a.pic_link', 'title'],
//            'url' => ['.list_su ul li .liimg a.pic_link', 'href'],
//        ])->queryData();
//        dump($data);
//        dump(\GuzzleHttp\json_encode(
//            [
//                'title' => ['.list_su ul li .liimg a.pic_link', 'title'],
//                'url' => ['.list_su ul li .liimg a.pic_link', 'href'],
//            ]
//        ));
//
//
//        $page = $ql->rules([
//            'page' => [".pages em~a:first","href"],
//        ])->queryData();
//        dump($page);
//
        $sources = $ql->get('http://www.btbtdy.me/vidlist/15446.html')->rules([
            'source' => ['ul > li > span > a', 'href'],
            'source_tex' => ['ul > li > span > a', 'text'],
        ])->queryData();
        dump($ql->getHtml());
        dump($sources);

        dump(\GuzzleHttp\json_encode([
            'content' => ['.vod_intro', 'html', 'a h1 -script -span.bitt -#bdshare  -:empty'],
            'link' => ['ul > li > span > a', 'href'],
            'link_text' => ['ul > li > span > a', 'text'],
        ]));

//        $content = $ql->get('http://www.btbtdy.me/btdy/dy15446.html')->rules([
//            'content' => ['.vod_intro', 'html', 'a h1 -script -span.bitt -#bdshare  -:empty'],
//        ])->query()->getData(function ($item) use($ql){
//            return $item;
//        });
//        dump($content);
    }


    public function test() {
        $url = 'https://img3.doubanio.com/misc/mixed_static/6509e3252aea080.js';

        $ql = QueryList::getInstance();
        $ql->get($url);

        $content = preg_replace('/\s+/', '', $ql->getHtml());

        preg_match('/varsources=\{\};(.*?)\$\(\'.playBtn\'\)\.click/', $content, $res);
        $content = preg_replace('/sources\[\d+\]=/', '', $res[1]);
        $res = explode(',];', $content);

        $sources = [];
        foreach ($res as $value) {
            if (empty($value)) {
                continue;
            }
            $value = $value. ']';
            $value = str_replace(['play_link', 'ep'],['"play_link"', '"ep"'], $value);
            dump(json_decode($value));
            //echo $value;exit();
        }
        dd(1);
        preg_match_all('/sources\[\d+\]=(\[.*\]);/', $content, $res);
        dd(count($res));

        $res[1] = str_replace([',]', ',}'],[']', '}'], $res[1]);
        $res[1] = str_replace(['play_link', 'ep'],['"play_link"', '"ep"'], $res[1]);
        $res[1] = json_decode($res[1]);
        dump($res[1]);
    }


}
